import csv
import time

import pymysql
import requests
import demjson
from pyquery import PyQuery as pq



def get_content(key):

    index_url = 'http://hotels.ctrip.com/Domestic/Tool/AjaxHotelList.aspx'
    header = {'Accept':'*/*',
    'Accept-Encoding':'gzip, deflate',
    'Accept-Language':'zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7',
    'Cache-Control':'max-age=0',
    'Connection':'keep-alive',
    'Content-Length':'2091',
    'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
    'Cookie':'Union=SID=155952&AllianceID=4897&OUID=baidu81|index|||; Session=SmartLinkCode=U155952&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=&SmartLinkLanguage=zh; _abtest_userid=7e26ec40-1632-4e71-89d4-f230da0a8682; gad_city=bfc57e4d16854aac15936b76ba41619a; _RF1=58.247.39.221; _RSG=2qKX3mYxqO1UIzBMCW1hB9; _RDG=2879745bf9f74027270297e6b40fc81ab9; _RGUID=1634ee2c-0385-4d02-92eb-0603162fd49e; traceExt=campaign=CHNbaidu81&adid=index; _ga=GA1.2.624262148.1537278555; _gid=GA1.2.879479468.1537278555; MKT_Pagesource=PC; appFloatCnt=1; manualclose=1; ASP.NET_SessionId=oy10isklemzztpi5uhqmlldy; OID_ForOnlineHotel=1537278551666lgfql1537278659884102002; HotelDomestic_CitySight=866=4203646; HotelDomesticVisitedHotels1=6442183=0,0,4.9,1587,/200n0t000000ibdbwB063.jpg,&8588665=0,0,4.9,553,/200e0u000000j0an67191.jpg,&10226881=0,0,5,353,/20080u000000jfds0D8FC.jpg,; Mkt_UnionRecord=%5B%7B%22aid%22%3A%224897%22%2C%22timestamp%22%3A1537279908067%7D%5D; _jzqco=%7C%7C%7C%7C1537278555242%7C1.1971121706.1537278554600.1537279807261.1537279908079.1537279807261.1537279908079.undefined.0.0.15.15; __zpspc=9.1.1537278554.1537279908.15%231%7Cbaidu%7Ccpc%7Cbaidu81%7C%25E6%2590%25BA%25E7%25A8%258B%7C%23; _bfa=1.1537278551666.lgfql.1.1537278551666.1537279804349.2.29; _bfs=1.8; _bfi=p1%3D102002%26p2%3D102002%26v1%3D29%26v2%3D28',
    'Host':'hotels.ctrip.com',
    'If-Modified-Since':'Thu, 01 Jan 1970 00:00:00 GMT',
    'Origin':'http://hotels.ctrip.com',
    'Referer':'http://hotels.ctrip.com/hotel/lijiang37',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36',
    }

    data = {'__VIEWSTATEGENERATOR':' DB1FBB6D',
'cityName':' %E4%B8%BD%E6%B1%9F',
'StartTime':' 2018-09-19',
'DepTime':' 2018-09-20',
'RoomGuestCount':' 1,1,0',
'txtkeyword':' ',
'Resource':' ',
'Room':' ',
'Paymentterm':' ',
'BRev':' ',
'Minstate':' ',
'PromoteType':' ',
'PromoteDate':' ',
'operationtype':' NEWHOTELORDER',
'PromoteStartDate':' ',
'PromoteEndDate':' ',
'OrderID':' ',
'RoomNum':' ',
'IsOnlyAirHotel':' F',
'cityId':' 37',
'cityPY':' lijiang',
'cityCode':' 0888',
'cityLat':' 26.8610773294',
'cityLng':' 100.2342228868',
'positionArea':' ',
'positionId':' ',
'hotelposition':' ',
'keyword':' ',
'hotelId':' ',
'htlPageView':' 0',
'hotelType':' F',
'hasPKGHotel':' F',
'requestTravelMoney':' F',
'isusergiftcard':' F',
'useFG':' F',
'HotelEquipment':' ',
'priceRange':' -2',
'hotelBrandId':' ',
'promotion':' F',
'prepay':' F',
'IsCanReserve':' F',
'OrderBy':' 99',
'OrderType':' ',
'k1':' ',
'k2':' ',
'CorpPayType':' ',
'viewType':' ',
'checkIn':' 2018-09-19',
'checkOut':' 2018-09-20',
'DealSale':' ',
'ulogin':' ',
'hidTestLat':' 0%7C0',
'AllHotelIds':' 3654763%2C6442183%2C2576684%2C2929424%2C1862244%2C15716994%2C6496332%2C1582515%2C8373961%2C708815%2C6802946%2C2058870%2C1103298%2C6223682%2C1667695%2C1583472%2C7366392%2C1743205%2C6561617%2C1213978%2C2088988%2C453332%2C15252544%2C1606899%2C6535047',
'psid':' ',
'isfromlist':' T',
'ubt_price_key':' htl_search_result_promotion',
'showwindow':' ',
'defaultcoupon':' ',
'isHuaZhu':' False',
'hotelPriceLow':' ',
'htlFrom':' hotellist',
'unBookHotelTraceCode':' ',
'showTipFlg':' ',
'traceAdContextId':' v2_H4sIAAAAAAAAAC3Muw3CMBSF4aRjBkpEhWLp%2Bvo%2BU7KIhR2oGYGSvbJHNmAHIsfl%2BXT0n7b189um83dEdOFc3zVH1ijulOOcdMLEAM3FAJ3k4OjSObIlUuy8L22MYOZmPcKs1JiIRbsKOB4JYFBKTS%2FD7YrFK5Bo4P0SqD6X4A95BS6eRJCswALjPNyHP7uSLCPAAAAA',
'allianceid':' 0',
'sid':' 0',
'hotelIds':' 3654763_1_1,6442183_2_1,2576684_3_1,2929424_4_1,1862244_5_1,15716994_6_1,6496332_7_1,1582515_8_1,8373961_9_1,708815_10_1,6802946_11_1,2058870_12_1,1103298_13_1,6223682_14_1,1667695_15_1,1583472_16_1,7366392_17_1,1743205_18_1,6561617_19_1,1213978_20_1,2088988_21_1,453332_22_1,15252544_23_1,1606899_24_1,6535047_25_1',
'markType':' 0',
'type':' ',
'brand':' ',
'group':' ',
'feature':' ',
'equip':' ',
'bed':' ',
'breakfast':' ',
'other':' ',
'star':' ',
'price':' ',
'a':' 0',
'keywordLat':' ',
'keywordLon':' ',
'contrast':' 0',
'PaymentType':' ',
'CtripService':' ',
'promotionf':' ',
'contyped':' 0',
'productcode':' ',
'attachDistance':' 0',
'zone':' 765',
'sl':' ',
'l':' ',
's':' ',
'location':' ',
'page':' 1',
        }
    for i in range(1,2):
        print(i)
        time.sleep(1)
        data['page'] = i
        resp = demjson.decode(requests.post(index_url,headers=header,data=data).text)
        hotel_list = resp.get("hotelPositionJSON")
        hotel_num = resp.get("HotelMaiDianData").get("value").get("hotelnum")
        time.sleep(1)
        for item in hotel_list:
            name = item.get("name")
            address = item.get("address")
            lat = item.get("lat")
            lon = item.get("lon")
            dpcount = item.get("dpcount")#多少住客
            dpscore = item.get("dpscore")#用户推荐数
            score = item.get("score")#综合分数
            shortName = item.get("shortName")#简称
            stardesc = item.get("stardesc")#酒店类型
            detail_url = 'http://hotels.ctrip.com'+item.get("url")
            time.sleep(1)
            second_doc = pq(requests.get(detail_url).text)
            data_list = second_doc("div.htl_room_txt.text_3l p").remove('span').text()
            datas = data_list.split('  ')
            tags = second_doc('div.special_label').text()
            build = datas[0]
            room_num = datas[-1]
            with open('{}_reslut.csv'.format(key), 'a', encoding='utf8', newline='') as s:
                writer = csv.writer(s)
                writer.writerow((hotel_num,name,address,lat,lon,dpcount,dpscore,score,shortName,stardesc,tags,build,room_num,key))
            print(hotel_num,name,address,lat,lon,dpcount,dpscore,score,shortName,stardesc,tags,build,room_num)

if __name__ == '__main__':
    get_content('白沙古镇')
    #http://hotels.ctrip.com/domestic/hotel/lijiang37